<?php

/**
 * news actions.
 *
 * @package    jmscargo
 * @subpackage news
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class newsActions extends sfActions
{
    
public function executeIndex(sfWebRequest $request)
  {
     
    $this->newss = Doctrine_Core::getTable('News')
      ->createQuery('a')
      ->execute();
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->news = Doctrine_Core::getTable('News')->
     find($this->getRequestParameter('id'));
    $this->news->increase(); // baazdaa View gesen field uusge
    $this->getUser()->setAttribute('LastNews',  $this->news->getTitle());
  }
  public function executeList(sfWebRequest $request)
  {
    $cat_id=$this->getRequestParameter('cat_id');
    $page_id=$this->getRequestParameter('page',1);
    
    $this->newslist = Doctrine_Core::getTable('News')
      ->selectByCategroyId($cat_id, $page_id);
    
   $this->category = Doctrine_Core::getTable('Category')
           ->find($this->getRequestParameter('id'));
  }
      public function executeSearch(sfWebRequest $request)
  {
    $query=$this->getRequestParameter('query');
    
    $this->newss= Doctrine_Core::getTable('News')
      ->createQuery('a')
      ->where('a.title like ?',"%".$query."%")
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
      $news= new News();
      $news->setUserId(7);
      $this->form = new NewsForm($news);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new NewsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object news does not exist (%s).', $request->getParameter('id')));
    $this->form = new NewsForm($news);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object news does not exist (%s).', $request->getParameter('id')));
    $this->form = new NewsForm($news);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($news = Doctrine_Core::getTable('News')->find(array($request->getParameter('id'))), sprintf('Object news does not exist (%s).', $request->getParameter('id')));
    $news->delete();

    $this->redirect('news/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $news=$form->save();
      $thumbnail = new sfThumbnail(100, 100,false,false,90);
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/news/'.$news->geturl());
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/news/s_'.$news->geturl());
      $this->redirect('news/show?id='.$form->getObject()->getId());
    }
  }
}
